#include "PlayerMgr.h"
#include "base/core/log.h"
#include "base/core/Profiler.h"
#include "Common/CharacterDefine.h"
#include "Common/Utility/ServerTime.h"
#include "ProtoBuffer/AllProtocol.h"
#include "../LogicService.h"
#include "LogicService.h"
#include "Scene/MapMgr.h"
#include "CreatureMgr.h"
#include "Duplicate/DuplicateMgr.h"

PlayerMgr::PlayerMgr() 
{
	m_req = 0;
	m_reqDB.clear();
}
PlayerMgr::~PlayerMgr()
{
}

bool PlayerMgr::Init()
{
	IMessageDispatch *pMessageDispatch = g_GetLogicService()->GetServiceMessageDispatch();
	if (nullptr == pMessageDispatch)
	{
		return false;
	}
	//DB
	pMessageDispatch->registerDBProxyHandler(EMODULE_ID_PLAYER, this);
	pMessageDispatch->registerDBProxyHandler(EMODULE_ID_LOGIN, this);

	//中心服
	pMessageDispatch->registerServerHandler(EMODULE_ID_LOGIN, this);
	pMessageDispatch->registerServerHandler(EMODULE_ID_RANKINGLIST, this);

	//客户端
	pMessageDispatch->registerClientHandler(EMODULE_ID_PLAYER, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_SCENE,this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_MOVE, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_SKILL, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_BUFF, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_PACKAGE, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_TASK, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_CHAT, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_DUPLICATE, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_ACHIEVEMENT, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_DEPUTY,this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_PVP, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_LOGIN, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_PLAYERPET, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_TREASURE, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_BREACH, this);
	pMessageDispatch->registerClientHandler(EMOUDLE_ID_COLLECT, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_RUNBUSINESS, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_WELFARE, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_TITLE,  this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_HUOYUEDU, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_OPERATELIMIT, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_HORSE, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_GOD, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_PANDOS, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_PAY, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_ADVENTURE, this);

	return true;
}

bool PlayerMgr::UnInit()
{
	m_req = 0;
	m_reqDB.clear();
	IMessageDispatch *pMessageDispatch = g_GetLogicService()->GetServiceMessageDispatch();
	if (nullptr != pMessageDispatch)
	{
		pMessageDispatch->unregisterDBProxyHandler(EMODULE_ID_PLAYER);
		pMessageDispatch->unregisterDBProxyHandler(EMODULE_ID_LOGIN);

		//中心服
		pMessageDispatch->unregisterServerHandler(EMODULE_ID_LOGIN);
		pMessageDispatch->unregisterServerHandler(EMODULE_ID_RANKINGLIST);

		pMessageDispatch->unregisterClientHandler(EMODULE_ID_PLAYER);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_SCENE);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_MOVE);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_SKILL);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_BUFF);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_PACKAGE);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_TASK);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_CHAT);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_DUPLICATE);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_ACHIEVEMENT);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_DEPUTY);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_PVP);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_LOGIN);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_PLAYERPET);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_TREASURE);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_BREACH);
		pMessageDispatch->unregisterClientHandler(EMOUDLE_ID_COLLECT);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_RUNBUSINESS);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_WELFARE);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_TITLE);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_HUOYUEDU);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_OPERATELIMIT);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_PANDOS);
		pMessageDispatch->unregisterClientHandler(EMODULE_ID_PAY);
	}

	return true;
}

void PlayerMgr::Update(uint64_t tick)
{
}

void PlayerMgr::onDBProxyMessage(uint32_t actionId, RECV_PACKAGE &package)
{
	switch (actionId)
	{
	case DBPROXY_PROTOCOL_GET_CHARACTER_DATA_RSP:	//保存角色详细信息DB返回
		CharDBRet(package);
		break;
	default:
		break;
	}
}

void PlayerMgr::onServerMessage(uint32_t actionId, RECV_PACKAGE &package)
{
	//中心服发的帐号消息
	if (EMODULE_ID_LOGIN == package.moduleId || package.moduleId == EMODULE_ID_RANKINGLIST)
		OnMsgHandle(package);
}

void PlayerMgr::onClientMessage(uint32_t actionId, RECV_PACKAGE& package, uint64_t Id /* = 0 */)
{
	Player* pPlayer = g_GetCreatureMgr()->GetPlayer(Id);
	if (!pPlayer)
	{
		LogErrFmtPrint("recv msg error : can not find player...moduleid:%d,actionid:%d,id:%lu ", package.moduleId,package.cmd,Id);
		return;
	}

	if (package.moduleId <= EMODULE_ID_NONE || package.moduleId >= EMODULE_ID_LIMIT)
	{
		LogErrFmtPrint("recv msg error : can not find moduleid ...cid:%lu, moduleid:%d,actionid:%d ", pPlayer->GetCid(), package.moduleId, package.cmd);
		return;
	}
	if (ACCOUNT_GAME_STATE_SWITCH == pPlayer->GetAccountState())
	{
		LogErrFmtPrint("PlayerMgr::onClientMessage....ACCOUNT_GAME_STATE_SWITCH == pPlayer->GetAccountState()...cid:%lu,moduleid:%d,actionid:%d,actionId:%u ", Id, package.moduleId, package.cmd, actionId);
		return;
	}

	if (EMODULE_ID_PLAYER == package.moduleId)
	{
		pPlayer->OnMsgHandle(actionId, package.dataAfterHead, package.sizeAfterHead);
		return;
	}

	uint16_t nPartId = PART_NONE;
	switch (package.moduleId)
	{
	case EMODULE_ID_MOVE:
		nPartId = PART_MOVE;
		break;
	case EMODULE_ID_SKILL:
		nPartId = PART_SKILL;
		break;
	case EMODULE_ID_BUFF:
		nPartId = PART_BUFF;
		break;
	case EMODULE_ID_PACKAGE:
		nPartId = PART_PACKAGE;
		break;
	case EMODULE_ID_TASK:
		nPartId = PART_MISSION;
		break;
	case EMODULE_ID_CHAT:
		nPartId = PART_CHAT;
		break;
	case EMODULE_ID_DUPLICATE:
		nPartId = PART_DUPLICATE;
		break;
	case EMODULE_ID_ACHIEVEMENT:
		nPartId = PART_ACHIEVEMENT;
		break;
	case EMODULE_ID_DEPUTY:
		nPartId = PART_DEPUTY;
		break;
	case EMODULE_ID_PVP:
		nPartId = PART_PVP;
		break;
	case EMODULE_ID_TREASURE:
		nPartId = PART_TREASURE;
		break;
	case EMOUDLE_ID_COLLECT:
		nPartId = PART_COLLECT;
		break;
	case EMODULE_ID_RUNBUSINESS:
		nPartId = PART_RUNBUSINESS;
		break;
	case EMODULE_ID_WELFARE:
		nPartId = PART_WELFARE;
		break;
	case EMODULE_ID_TITLE:
		nPartId = PART_TITLE;
		break;
	case EMODULE_ID_OPERATELIMIT:
		nPartId = PART_OPERATELIMIT;
		break;
	case EMODULE_ID_HUOYUEDU:
		nPartId = PART_HUOYUEDU;
		break;
	case EMODULE_ID_HORSE:
		nPartId = PART_HORSE;
		break;
	case EMODULE_ID_GOD:
		nPartId = PART_GOD;
		break;
	case EMODULE_ID_PANDOS:
		nPartId = PART_PANDOS;
		break;
	case EMODULE_ID_PAY:
		nPartId = PART_PAY;
		break;
	case EMODULE_ID_ADVENTURE:
		nPartId = PART_ADVENTURE;
		break;
	default:
		break;
	}
	Part *pPart = pPlayer->GetPart(nPartId);
	if (nullptr != pPart)
	{
		pPart->OnMsgHandle(actionId, package.dataAfterHead, package.sizeAfterHead);
	}

}

void PlayerMgr::OnMsgHandle(RECV_PACKAGE& package)
{
	switch (package.cmd)
	{
	case CENTER_LOGIN_LOGIC_REQ:
		EnterLogicReq(package);
		break;
	case CENTER_LOGINOUT_LOGIC_REQ:
		LeaveLogicReq(package);
		break;
	case CENTER_LOGIC_RSP_RANKINGLIST_INDEX:
		OnRankingList(package);
		break;
	default:
		break;
	}
}

//Player* PlayerMgr::GetPlayer(CharIDType PlayerId)
//{
//	//if (all)
//	//{
//	//	DELPLAYER_MAP::iterator delIter = m_delPlayerMap.find(PlayerId);
//	//	DELPLAYER_MAP::iterator delIterEnd = m_delPlayerMap.end();
//	//	if (delIter != delIterEnd)
//	//		return nullptr;
//	//}
//
//	PlayerObjMap::iterator iter = m_mapPlayer.find(PlayerId);
//	if (iter == m_mapPlayer.end())
//		return nullptr;
//	return iter->second;
//}

//void PlayerMgr::RecylePlayer(CharIDType playerId)
//{
//	PlayerObjMap::iterator iter = m_mapPlayer.find(playerId);
//	if (iter == m_mapPlayer.end())
//		return;
//
//	Player *pPlayer = iter->second;
//	m_mapPlayer.erase(iter);
//	if (!pPlayer)
//		return;
//
//	pPlayer->UnInit();
//	g_GetCreatureMgr()->RecyleCreature(pPlayer);
//	//m_pPlayerPool->FreeObj(pPlayer);
//	pPlayer = nullptr;
//}

//bool PlayerMgr::CreatePlayer(CharLoginInfo* pLoginInfo, CharacterDBInfo &playerInfo)
//{
//	Player *pPlayer = dynamic_cast<Player*>(g_GetCreatureMgr()->MallocCreature(CREATURE_PLAYER));
//	//Player *pPlayer = m_pPlayerPool->MallocObj();
//	if (!pPlayer || !pPlayer->Init(playerInfo, pLoginInfo))
//	{
//		LogErrFmtPrint("create player fail, playerid:", playerInfo.charid());
//		return false;
//	}
//	m_mapPlayer[playerInfo.charid()] = pPlayer;
//	if (false == pPlayer->LoginInGame())
//	{
//		RecylePlayer(playerInfo.charid());
//		return false;
//	}
//
//	m_onlineList.AddTail(pPlayer->GetHostNode());
//
//	return true;
//}
//
//bool PlayerMgr::CreatePlayer(CharLoginInfo* pLoginInfo, CharacterDBInfo &playerInfo, bool change)
//{
//	Player *pPlayer = dynamic_cast<Player*>(g_GetCreatureMgr()->MallocCreature(CREATURE_PLAYER));
//	//Player *pPlayer = m_pPlayerPool->MallocObj();
//	if (!pPlayer || !pPlayer->Init(playerInfo, pLoginInfo))
//	{
//		LogErrFmtPrint("[logic] PlayerMgr::CreatePlayer....create player fail, playerid:", playerInfo.charid());
//		return false;
//	}
//	m_mapPlayer[playerInfo.charid()] = pPlayer;
//	if (false == pPlayer->LoginInGame(change))
//	{
//		RecylePlayer(playerInfo.charid());
//		return false;
//	}
//
//	m_onlineList.AddTail(pPlayer->GetHostNode());
//
//	return true;
//}

//int PlayerMgr::GetPlayerCount()
//{
//	return m_mapPlayer.size();
//}

//Part* PlayerMgr::CreatePart(uint32_t partType, Creature *pCreature, CharacterDBInfo *playerInfo /* = nullptr */)
//{
//	Part *pPart = nullptr;
//	switch (partType)
//	{
//	case PART_MOVE:
//		pPart = m_pMovePool->MallocObj();
//		break;
//	case PART_SKILL:
//		pPart = m_pSkillPool->MallocObj();
//		break;
//	case PART_BUFF:
//		pPart = m_pBuffPool->MallocObj();
//		break;
//	case PART_PACKAGE:
//		pPart = m_pPackagePool->MallocObj();
//		break;
//	case PART_MISSION:
//		pPart = m_pMissionPoll->MallocObj();
//		break;
//	case PART_CHAT:
//		pPart = m_pChatPoll->MallocObj();
//		break;
//	case PART_DUPLICATE:
//		pPart = m_pDuplicatePool->MallocObj();
//		break;
//	case PART_ACHIEVEMENT:
//		pPart = m_pAchievementPool->MallocObj();
//		break;
//	case PART_DEPUTY:
//		pPart = m_pDeputyPool->MallocObj();
//		break;
//	case PART_PVP:
//		pPart = m_pPvpPool->MallocObj();
//		break;
//	case PART_PET:
//		pPart = m_pPlayerPetPool->MallocObj();
//		break;
//	case PART_TREASURE:
//		pPart = m_pTreasurePool->MallocObj();
//		break;
//	case PART_BREACH:
//		pPart = m_pBreachPool->MallocObj();
//		break;
//	case PART_COLLECT:
//		pPart = m_pCollectPool->MallocObj();
//		break;
//	default:
//		break;
//	}
//	if (!pPart || !pPart->Init(pCreature, partType, playerInfo))
//		return nullptr;
//	return pPart;
//}

//void PlayerMgr::RecylePart(Part *pPart)
//{
//	if (!pPart)
//	{
//		return;
//	}
//
//	uint32_t partType = pPart->getPartType();
//	if (!pPart->UnInit())
//		return;
//
//	switch (partType)
//	{
//	case PART_MOVE:
//		m_pMovePool->FreeTrack(dynamic_cast<MovePart*>(pPart));
//		break;
//	case PART_SKILL:
//		m_pSkillPool->FreeTrack(dynamic_cast<SkillPart*>(pPart));
//		break;
//	case PART_BUFF:
//		m_pBuffPool->FreeTrack(dynamic_cast<BuffPart*>(pPart));
//		break;
//	case PART_PACKAGE:
//		m_pPackagePool->FreeTrack(dynamic_cast<PackagePart*>(pPart));
//		break;
//	case PART_MISSION:
//		m_pMissionPoll->FreeTrack(dynamic_cast<MissionPart*>(pPart));
//		break;
//	case PART_CHAT:
//		m_pChatPoll->FreeTrack(dynamic_cast<ChatPart*>(pPart));
//		break;
//	case PART_DUPLICATE:
//		m_pDuplicatePool->FreeTrack(dynamic_cast<DuplicatePart*>(pPart));
//		break;
//	case PART_ACHIEVEMENT:
//		m_pAchievementPool->FreeTrack(dynamic_cast<AchievementPart*>(pPart));
//		break;
//	case PART_DEPUTY:
//		m_pDeputyPool->FreeTrack(dynamic_cast<DeputyPart*>(pPart));
//		break;
//	case PART_PVP:
//		m_pPvpPool->FreeTrack(dynamic_cast<PvpPart*>(pPart));
//		break;
//	case PART_PET:
//		m_pPlayerPetPool->FreeTrack(dynamic_cast<PlayerPetPart*>(pPart));
//		break;
//	case PART_TREASURE:
//		m_pTreasurePool->FreeTrack(dynamic_cast<TreasurePart*>(pPart));
//		break;
//	case PART_BREACH:
//		m_pBreachPool->FreeTrack(dynamic_cast<BreachPart*>(pPart));
//		break;
//	case PART_COLLECT:
//		m_pCollectPool->FreeTrack(dynamic_cast<CollectPart*>(pPart));
//		break;
//	default:
//		break;
//	}
//}

//bool PlayerMgr::SendToAllPlayers(uint32_t cmd, ProtoBufBase* buf)
//{
//	Player *pPlayer = nullptr;
//	PlayerObjMap::iterator iter = m_mapPlayer.begin();
//	for (; iter != m_mapPlayer.end(); iter++)
//	{
//		pPlayer = iter->second;
//		if (pPlayer)
//			pPlayer->SendDataToClient(cmd, buf);
//	}
//	return true;
//}
//
//bool PlayerMgr::SendToAllPlayers(char* buf, uint32_t size, uint32_t groupId)
//{
//	//Player *pPlayer = nullptr;
//	//PlayerObjMap::iterator iter = m_mapPlayer.begin();
//	//for (; iter != m_mapPlayer.end(); iter++)
//	//{
//	//	pPlayer = iter->second;
//	//	if (pPlayer)
//	//		pPlayer->SendDataToClient(buf, size);
//	//}
//	return true;
//}

bool PlayerMgr::EnterLogicReq(RECV_PACKAGE& package)
{
	bool result = false;
	bool retCode = false;
	CenterLoginLogicReq gatelogin;
	retCode = gatelogin.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
	MMO_PROCESS_ERROR(retCode);
	{
		uint32_t id = g_GetLogicService()->GetID();
		CharIDType cid = gatelogin.cid();
		CharLoginInfo loginInfo;
		loginInfo.uid = gatelogin.uid();
		loginInfo.sceneid = gatelogin.sceneid();
		loginInfo.mapid = gatelogin.mapid();
		loginInfo.clientId = gatelogin.clientid();
		loginInfo.x = gatelogin.pos().x();
		loginInfo.y = gatelogin.pos().y();
		loginInfo.z = gatelogin.pos().z();
		loginInfo.gateId = gatelogin.gateid();
		loginInfo.channelId = gatelogin.channelid();
		loginInfo.protoLogin.CopyFrom(gatelogin.center_data());
		loginInfo.transParam.transType = ETransType_Born;
		loginInfo.finishFlag = gatelogin.finish_flag();
		loginInfo.finishStep = gatelogin.finish_step();
		loginInfo.guideDupId = gatelogin.guide_dup();

		LogDebugFmtPrint("[logic] PlayerMgr::EnterLogicReq....id:%u uid:%u, charid:%llu, entersceneid:%u, entermapid:%u,[%f,%f,%f],finishflag:%u,finishstep:%u,guidedup:%u ",
			id,loginInfo.uid,cid,loginInfo.sceneid,loginInfo.mapid,loginInfo.x, loginInfo.y, loginInfo.z,loginInfo.finishFlag,loginInfo.finishStep,loginInfo.guideDupId);

		if (!loginInfo.finishFlag)
		{
			//新手引导副本没有完成，需要先创建一个副本
			IDuplicate *pDuplicate = g_GetDuplicateMgr()->CreateGuideDup(loginInfo.sceneid, loginInfo.mapid, gatelogin.dup_param());
			if (nullptr == pDuplicate)
			{
				MMOLOG_FMT_ERROR("PlayerMgr::EnterLogicReq...id:%u,cid:%lu,sceneid:%u,mapid:%u ", id, cid, loginInfo.sceneid, loginInfo.mapid);
				goto Exit0;
			}
			else
			{
				LogDebugFmtPrint("PlayerMgr::EnterLogicReq...CreateGuideDup success..id:%u,cid:%lu,sceneid:%u,mapid:%u,dupid:%u ", id, cid, loginInfo.sceneid, loginInfo.mapid,pDuplicate->GetDuplicateID());
			}
		}
		//
		retCode = EnterGame(cid, loginInfo);
		MMO_PROCESS_ERROR(retCode);
		result = true;
	}

Exit0:
	return result;
}

bool PlayerMgr::LeaveLogicReq(RECV_PACKAGE& package)
{
	bool result = false;
	bool retCode = false;
	{
		GateLoginOutLogicSceneReq gateloginout;
		retCode = gateloginout.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
		MMOLOG_PROCESS_ERROR(retCode);

		CharIDType cid = gateloginout.cid();
		uint32_t uid = gateloginout.uid();
		uint32_t clientId = gateloginout.clientid();
		LOGOUT_TYPE type = gateloginout.type();
		retCode = LeaveGame(cid, uid, clientId, type);
		MMOLOG_PROCESS_ERROR(retCode);

		result = true;
	}
Exit0:
	return result;
}

bool PlayerMgr::OnRankingList(RECV_PACKAGE& package)
{
	bool result = false;
	bool retCode = false;
	{
		CenterToLogicRspRankingList rsp;
		retCode = rsp.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
		MMOLOG_PROCESS_ERROR(retCode);

		CharIDType cid = rsp.cid();
		uint32_t type = rsp.type();
		uint32_t index = rsp.index();
		Player * pPlayer=g_GetCreatureMgr()->GetPlayer(cid);
		if (pPlayer)
		{
			pPlayer->ReqCenterRankingListIndex(type,index);
		}

		result = true;
	}
Exit0:
	return result;
}


uint32_t PlayerMgr::NewReq()
{
	if (m_req >= UINT32_MAX)
	{
		m_req = 0;
	}
	++m_req;

	return m_req;
}

CharLoginInfo *PlayerMgr::GetCharLogin(CharIDType cid)
{
	CHAR_LOGIN_MAP::iterator iter = m_loginMap.find(cid);
	return (iter != m_loginMap.end()) ? &iter->second : nullptr;
}

void PlayerMgr::DelCharLogin(CharIDType cid)
{
	m_loginMap.erase(cid);
}

bool PlayerMgr::CharDBRet(RECV_PACKAGE& package)
{
	bool result = false;
	bool retCode = false;
	uint32_t rspreqid = 0;
	uint32_t reqid = 0;
	CharIDType cid = 0;
	//uint32_t uid = 0;
	bool change = false;
	CharacterDBInfo *pCharDBInfo = nullptr;
	CharacterDBBaseInfo *pCharDBBaseInfo = nullptr;
	CharLoginInfo *pLogin = nullptr;
	{
		uint32_t sid = g_GetLogicService()->GetID();
		CharacterInfoResponse rspDB;
		retCode = rspDB.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
		MMOLOG_PROCESS_ERROR(retCode);

		cid = rspDB.reqcharid();
		rspreqid = rspDB.requestid();
		LogDebugFmtPrint("[logic] PlayerMgr::CharDBRet result:%d ,reqid:%u  charid:%llu, sid:%u ", rspDB.result(), rspreqid, rspDB.reqcharid(), sid);
		pLogin = GetCharLogin(cid);
		if (nullptr == pLogin)
		{
			LogErrFmtPrint("[logic] PlayerMgr::CharDBRet....nullptr == pLogin...cid:%llu , sid:%u ",cid,sid);
			MMOLOG_PROCESS_ERROR(false);
		}
		reqid = pLogin->reqid;
		if (rspreqid != reqid)
		{
			LogErrFmtPrint("[logic] PlayerMgr::CharDBRet....rspreqid != reqid...cid:%llu , sid:%u, rspreqid:%u, reqid:%u  ", cid, sid, rspreqid, reqid);
			MMOLOG_PROCESS_ERROR(false);
		}
		//
		if (CharacterInfoResponse::SUCCESS == rspDB.result())
		{
			if (!rspDB.has_charinfo())
			{
				LogErrFmtPrint("[logic] PlayerMgr::CharDBRet.....rspDB.has_charinfo() return false....charid:%llu, reqid:%u, rspid:%u ", cid, reqid, rspDB.requestid());
				MMOLOG_PROCESS_ERROR(false);
			}
			if (rspDB.has_change() && rspDB.change() > 0)
			{
				change = true;
			}

			pCharDBInfo = rspDB.mutable_charinfo();
			pCharDBBaseInfo = pCharDBInfo->mutable_basedata();			
			//
			uint32_t dbmapId = pCharDBBaseInfo->enter_map_id();
			uint32_t dbsceneId = pCharDBBaseInfo->enter_scene_id();
			if (!g_GetMapMgr()->IsDynamic(dbmapId))
			{
				pCharDBBaseInfo->set_lastmapid(dbmapId);
				pCharDBBaseInfo->set_lastsceneid(dbsceneId);
				pCharDBBaseInfo->set_lastposx(pCharDBBaseInfo->enterposx());
				pCharDBBaseInfo->set_lastposy(pCharDBBaseInfo->enterposy());
				pCharDBBaseInfo->set_lastposz(pCharDBBaseInfo->enterposz());
			}
			//
			pCharDBBaseInfo->set_enter_map_id(pLogin->mapid);
			pCharDBBaseInfo->set_enter_scene_id(pLogin->sceneid);
			pCharDBBaseInfo->set_enterposx(pLogin->x);
			pCharDBBaseInfo->set_enterposy(pLogin->y);
			pCharDBBaseInfo->set_enterposz(pLogin->z);			

			//g_GetSceneMgr()->TransLogin(cid, pCharDBInfo->mutable_basedata());
			Player *pPlayer = g_GetCreatureMgr()->CreatePlayer(pLogin, *pCharDBInfo, change);
			if (nullptr == pPlayer)
			{
				LogErrFmtPrint("[logic] PlayerMgr::CharDBRet.....CreatePlayer return false....charid:%llu, reqid:%u, rspid:%u ", cid, reqid, rspDB.requestid());
				MMOLOG_PROCESS_ERROR(false);
			}
			//uid = atoi(pCharDBInfo->basedata().userid().c_str());
			//uid = pLogin->uid;
			if (!change)
			{
				//登陆信息发送到中心服
				retCode = EnterGameRspToCenter(pPlayer);
				MMOLOG_PROCESS_ERROR(retCode);
				if (false == pPlayer->LoginInGame(*pLogin, false))
				{
					//进入游戏失败的话 该接口会去移除在线列表
					g_GetCreatureMgr()->RecylePlayer(pPlayer->GetCid());
					return nullptr;
				}
				

			}
			else
			{
				//切逻辑节点,暂时不处理
			}
		}

	}

	result = true;
Exit0:
	DelCharLogin(cid);
	return result;
}

bool PlayerMgr::EnterGameRspToCenter(Player *pPlayer)
{
	bool result = false;
	bool retCode = false;
	//
	uint32_t uid = 0;
	uint32_t clientId = 0;
	CharIDType cid = 0; 
	CenterCharacterInfo *centerPackage = nullptr;
	//
	CenterLoginLogicRsp login;
	MMOLOG_PROCESS_ERROR(nullptr != pPlayer);

	uid = pPlayer->GetUid();
	clientId = pPlayer->GetClientId();
	cid = pPlayer->GetCid();

	login.set_cid(cid);
	login.set_uid(uid);
	login.set_clientid(clientId);
	centerPackage = login.mutable_centercharacter();

	retCode = pPlayer->CenterCharacterPack(centerPackage);
	MMOLOG_PROCESS_ERROR(retCode);

	retCode = g_GetLogicService()->SendDataToCenter(EMODULE_ID_LOGIN, LOGIC_LOGIN_CENTER_RSP, &login);
	MMOLOG_PROCESS_ERROR(retCode);

	//先把登陆信息发到中心服，然后发给客户端初始化信息，不然中心服没有对应的cid到client表


	retCode = pPlayer->SynLoginInfo();
	MMOLOG_PROCESS_ERROR(retCode);

	//CenterLoginLogicRsp 消息中已经有战力和等级，这里没必要在同步一次
	//pPlayer->SyncToCenter(C_FIGHT_POWER);
	//pPlayer->SyncToCenter(C_LEVEL);
	result = true;
Exit0:
	return result;
}

bool PlayerMgr::EnterGame(CharIDType cid, CharLoginInfo& loginInfo, bool changeLogic /* = false */)
{
	LogInfoFmtPrint("PlayerMgr::EnterGame start ");
	bool result = false;
	bool retCode = false;
	uint32_t id = g_GetLogicService()->GetID();
	Player* pPlayer = g_GetCreatureMgr()->GetNoScenePlayer(cid);
	if (pPlayer == nullptr)
	{
		uint32_t newReq = NewReq();
		CharacterInfoRequest charDBReq;
		charDBReq.set_requestid(newReq);
		charDBReq.set_charid(cid);
		charDBReq.set_change(0);
		if (changeLogic)
		{
			charDBReq.set_change(1);
		}
		retCode = g_GetLogicService()->SendDataToDBProxy(EMODULE_ID_LOGIN, DBPROXY_PROTOCOL_GET_CHARACTER_DATA_REQ, &charDBReq);
		MMOLOG_PROCESS_ERROR(retCode);
		
		loginInfo.reqid = newReq;
		m_loginMap[cid] = loginInfo;

		LogDebugFmtPrint("[logic] PlayerMgr::EnterGame...DB Req  id:%u, uid:%u, cid:%llu, changeLogic:%d, dbreqid:%u,finishflag:%u,finishstep:%u,guiddup:%u ", id, loginInfo.uid, cid, (int32_t)changeLogic, newReq, loginInfo.finishFlag, loginInfo.finishStep, loginInfo.guideDupId);
	}
	else
	{
		LogDebugFmtPrint("[logic] PlayerMgr::EnterGame...id:%u, uid:%u, cid:%llu, changeLogic:%d,finishflag:%u,finishstep:%u,guiddup:%u  ", id, loginInfo.uid, cid, (int32_t)changeLogic,loginInfo.finishFlag,loginInfo.finishStep,loginInfo.guideDupId);
		//登陆信息发送到中心服
		retCode = EnterGameRspToCenter(pPlayer);
		MMOLOG_PROCESS_ERROR(retCode);
		retCode = pPlayer->LoginInGame(loginInfo,changeLogic);
		MMOLOG_PROCESS_ERROR(retCode);
		if (!changeLogic)
		{
			
		}
		LogInfoFmtPrint("PlayerMgr::EnterGame 2  ");
	}


	result = true;
Exit0:
	return result;
}

bool PlayerMgr::LeaveGame(CharIDType cid, uint32_t uid, uint32_t clientId, LOGOUT_TYPE type)
{
	bool result = false;
	bool retCode = false;
	uint32_t sid = g_GetLogicService()->GetID();
	//Player* pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	Player* pPlayer = g_GetCreatureMgr()->GetNoScenePlayer(cid);
	MMOLOG_PROCESS_ERROR(pPlayer != nullptr);

	retCode = pPlayer->Logout(type);
	MMOLOG_PROCESS_ERROR(retCode);

	g_GetCreatureMgr()->RecylePlayer(cid);

	result = true;

Exit0:
	LogDebugFmtPrint("[logic] PlayerMgr::LeaveGame....id:%u, uid:%u , cid :%llu, logouttype:%d,  ", sid, uid, cid, (int32_t)type);
	DelCharLogin(cid);

	CenterLoginOutLogicSceneRsp logoutRsp;
	logoutRsp.set_cid(cid);
	logoutRsp.set_uid(uid);
	logoutRsp.set_forceleave(false);
	logoutRsp.set_clientid(clientId);

	if (LOGOUT_TYPE_CHANGE_LOGIC != type)
	{
		retCode = g_GetLogicService()->SendDataToCenter(EMODULE_ID_LOGIN, LOGIC_LOGINOUT_CNETER_RSP, &logoutRsp);
		MMOLOG_PROCESS_ERROR(retCode);
	}

	return result;
}

void PlayerMgr::L2C_AddPrestige(MAP_UINT64_INT32 &mapAttr)
{
	LogicToCenter_AddCampAttr notify;
	MAP_UINT64_INT32::iterator iterchar = mapAttr.begin();
	for (; iterchar != mapAttr.end(); ++iterchar)
	{
		LogicToCenter_AddCampAttr_CampAttr *proto = notify.add_camp_attr();
		if (nullptr != proto)
		{
			proto->set_char_id(iterchar->first);
			proto->set_prestige(iterchar->second);
		}
	}
	g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_ADD_CAMP_ATTR, &notify);
}
